package com.ymtms.arrivalofgoods.service.impl;

import com.ymtms.arrivalofgoods.constant.ArriveConstant;
import com.ymtms.arrivalofgoods.dao.*;
import com.ymtms.arrivalofgoods.exception.CheckException;
import com.ymtms.arrivalofgoods.pojo.*;
import com.ymtms.arrivalofgoods.service.CheckGoodsCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Date;

@Service
public class CheckGoodsCustomerServiceImpl implements CheckGoodsCustomerService {
    @Autowired
    private CheckGoodsCustomerMapper checkGoodsCustomerMapper;
    @Autowired
    private GoodsbillReceoptMainMapper goodsbillReceoptMainMapper;
    @Autowired
    private CheckGoodsReceiveMapper checkGoodsReceiveMapper;
    @Autowired
    private GoodsbillMasterMapper goodsbillMasterMapper;
    @Autowired
    private DriverInformationMapper driverInformationMapper;

    @Autowired
    private CustomerReceiptInformationMapper customerReceiptInformationMapper;
    @Autowired
    private GoodsReceiptInformationMapper goodsReceiptInformationMapper;
    @Override
    public void modifyStatusByNum(Integer num) {


            CheckGoodsReceive checkGoodsReceive = checkGoodsReceiveMapper.queryByReceiptId(num);
            if(ArriveConstant.unchecked.equals(checkGoodsReceive.getStatus())){
                throw new CheckException("收货员还未验收");
            }
            GoodsbillReceoptMain goodsbillReceoptMain = goodsbillReceoptMainMapper.selectByPrimaryKey(num);

            if (ArriveConstant.unsettled.equals(goodsbillReceoptMain.getIfBalance())){
                throw new CheckException("请结算后再验收");
            }
            checkGoodsCustomerMapper.updateCheckCustomerByNum(num);

    }


    @Override
    public void insertReceiptHistory(Integer grmid) {

        GoodsbillReceoptMain goodsbillReceoptMain = goodsbillReceoptMainMapper.selectByPrimaryKey(grmid);
        GoodsbillMaster goodsbillMaster = goodsbillMasterMapper.selectByPrimaryKey(goodsbillReceoptMain.getGoodsBillpkid());
        DriverInformation driverInformation = driverInformationMapper.selectByPrimaryKey(goodsbillReceoptMain.getDriverPkid());

        //插入客户回执表
        CustomerReceiptInformation customerReceiptInformation = new CustomerReceiptInformation();
        customerReceiptInformation.setGoodsBillCode(goodsbillMaster.getGoodsBillCode());
        customerReceiptInformation.setCustomer(goodsbillMaster.getSendGoodsCustomer());
        customerReceiptInformation.setCheckGoodsRecord(ArriveConstant.checked);
        customerReceiptInformation.setReceiveGoodsPerson(goodsbillReceoptMain.getLinkmanMobile());
        customerReceiptInformation.setReceiveGoodsDate(new Date());
        customerReceiptInformation.setCarryBillEventPkid(0);
        customerReceiptInformationMapper.insertSelective(customerReceiptInformation);
        //插入货物回执表
        GoodsReceiptInformation goodsReceiptInformation = new GoodsReceiptInformation();
        goodsReceiptInformation.setGoodsRevertCode(goodsbillReceoptMain.getGoodsRevertBillCode());
        goodsReceiptInformation.setDriverName(driverInformation.getDriverName());
        goodsReceiptInformation.setCheckGoodsRecord(ArriveConstant.checked);
        goodsReceiptInformation.setReceiveGoodsPerson(goodsbillReceoptMain.getReceiveGoodsLinkman());
        goodsReceiptInformation.setReceiveGoodsDate(new Date());
        goodsReceiptInformationMapper.insertSelective(goodsReceiptInformation);

    }

}
